<?php

@session_start();

class Admin_Controllers_Index extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        $this->view->render('index/index');
    }

    public function login() {
        $model_user = new Admin_Model_User();
        if (isset($_POST['login'])) {
            $email = $_POST['email'];
            $pass = $_POST['pass'];
            $checkLogin = $model_user->checkLogin($email, $pass);
            if ($checkLogin == true) {
                $user = $model_user->getUser($email, $pass);
                $_SESSION['user'] = array('id' => $user[0]['id'], 'name' => $user[0]['user_name'], 'email' => $user[0]['user_email'], 'pass' => $user[0]['user_pass']);
                header("location:home");
            } elseif ($checkLogin == false) {
                $_SESSION['check'] = 1;
                $_SESSION['email'] = $_POST['email'];
                $_SESSION['pass'] = $_POST['pass'];
                header("location:index");
            }
        }
    }

    public function home() {
        $this->view->render('index/home');
    }

    public function logout() {
        unset($_SESSION['user']);
        $this->view->render('index/index');
    }

    public function update() {
        $model_user = new Admin_Model_User();
        if (isset($_POST['update'])) {
            $id = $_SESSION['user']['id'];
            $name = $_POST['name'];
            $email = $_POST['email'];
            $data = array('user_name' => $name, 'user_email' => $email);
            $testUpdate = $model_user->updateUser($data, $id);
            if ($testUpdate != -1) {
                $_SESSION['testUpdate'] = 1;
                $_SESSION['user']['name'] = $name;
                header("location:home");
            } else {
                $_SESSION['testUpdate'] = 2;
                header("location:home");
            }
        }
    }

    public function change() {
        $model_user = new Admin_Model_User();
        if (isset($_POST['change'])) {
            $id = $_SESSION['user']['id'];
            $new_pass = $_POST['pass_new'];
            $data = array('user_pass' => $new_pass);
            $testChange = $model_user->updateUser($data, $id);
            if ($testUpdate != -1) {
                $_SESSION['testChange'] = 1;
                $_SESSION['user']['pass'] = $new_pass;
                header("location:home");
            } else {
                $_SESSION['testChange'] = 2;
                header("location:home");
            }
        }
    }

}

?>
